<template>
  <!-- 摄像头列表组件 -->
  <div class="allcameramain">
    <!-- 摄像头控制按钮 -->
    <div class="one">摄像头操作区</div>
    <div
      v-if="
        cameraInfoList &&
          cameraInfoList.controlFlag &&
          judgePermission(3, 'control')
      "
      class="cameracontrol"
    >
      <div class="controne">
        <div class="contro-m z-top" @click="startcontrol(0)">
          <i class="el-icon-caret-top"></i>
        </div>
        <div class="contro-m z-left" @click="startcontrol(2)">
          <i class="el-icon-caret-left"></i>
        </div>
        <div class="contro-m z-right" @click="startcontrol(3)">
          <i class="el-icon-caret-right"></i>
        </div>
        <div class="contro-m z-bottom" @click="startcontrol(1)">
          <i class="el-icon-caret-bottom"></i>
        </div>
        <div class="contro-m z-mirror" @click="mirrorcontrol()">
          <i class="el-icon-refresh-right"></i>
        </div>
      </div>
      <div class="controtwo">
        <div class="contro-left" @click="startcontrol(10)">
          焦距
          <i class="el-icon-plus"></i>
        </div>
        <div class="contro-right" @click="startcontrol(11)">
          焦距
          <i class="el-icon-minus"></i>
        </div>
      </div>
      <div class="controthree">
        <div class="greenbut" @click="startcontrol(8)">
          <i class="el-icon-zoom-in"></i> 放大
        </div>
        <div class="greenbut" @click="startcontrol(9)">
          <i class="el-icon-zoom-out"></i>缩小
        </div>
      </div>
    </div>
    <div v-else class="cameracontrol1">
      <!-- <div class="uncontrolsty-line">此设备不可控</div> -->
      <div class="controne">
        <div class="contro-m z-top">
          <i class="el-icon-caret-top"></i>
        </div>
        <div class="contro-m z-left">
          <i class="el-icon-caret-left"></i>
        </div>
        <div class="contro-m z-right">
          <i class="el-icon-caret-right"></i>
        </div>
        <div class="contro-m z-bottom">
          <i class="el-icon-caret-bottom"></i>
        </div>
        <div class="contro-m z-mirror">
          <i class="el-icon-refresh-right"></i>
        </div>
      </div>
      <div class="controtwo">
        <div class="contro-left">
          焦距
          <i class="el-icon-plus"></i>
        </div>
        <div class="contro-right">
          焦距
          <i class="el-icon-minus"></i>
        </div>
      </div>
      <div class="controthree">
        <div class="greenbut">
          <i class="el-icon-zoom-in"></i> 放大
        </div>
        <div class="greenbut">
          <i class="el-icon-zoom-out"></i>缩小
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import nowpagereq from "@/api/processing-management/camera-manage/index";
import { mapGetters } from "vuex";
import Axios from "axios";
// import axios from 'axios'
export default {
  props: {
    cameraId: {
      type: [String, Number],
      default: ""
    },
    cameralistparam: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      // 当前摄像头id
      // cameraId: "",
      // 当前选中摄像头的详细信息
      cameraInfoList: {},
      // 网页播放地址
      cameraurl: "",
      startUp: "",
      openSourceMark: "",
      timer: null, // 定时器
      timer1: null // 定时器1
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {
    cameralistparam(newval) {
      this.getcameralist();
    },
    cameraId(id) {
      this.getcamermes(id);
    }
  },
  mounted() {},
  beforeDestroy() {
    clearInterval(this.cameraTimer)
  //   // 离开界面销毁预热
  //   // this.closePreheat();
  //   clearTimeout(this.timer);
  //   clearTimeout(this.timer1);
  //   this.timer = null;
  //   this.timer1 = null;
  //   this.cameraId = "";
  //   this.openSourceMark = "";
  },
  methods: {
    // 获取某个摄像头详细信息
    async getcamermes(id, newStartUp) {
      this.timer1 = null;
      clearTimeout(this.timer1);
      this.$emit("nowcameraurl", "transCamera");
      // if (this.startUp === "true") {
      //   this.closePreheat(this.cameraId);
      //   console.log("销毁预热");
      // }
      if (id === -1) {
        this.$emit("nowcameraurl", "notCameraList");
        console.log("列表无摄像头");
        this.cameraId = id;
        this.startUp = newStartUp;
        return;
      }
      this.cameraId = id;
      this.startUp = newStartUp;
      // 注意：startUp是字符串，不是Boolean
      // if (newStartUp === "true") {
      //   this.openSourceMark = new Date().getTime();
      //   await Axios({
      //     method: "post",
      //     url:
      //       this.$cloudBaseurl + "solarEnergyCamera/openPower",
      //     data: { cameraId: id, openSourceMark: this.openSourceMark }
      //   }).then(
      //     res => {
      //       console.log(res);
      //       // 子向父传值
      //       this.$emit("getUploadTime", res.data.data.openTime, this.startUp);
      //       console.log("预热加载");
      //       this.timer1 = setTimeout(() => {
      //         this.getSame(id);
      //       }, res.data.data.openTime * 60000);
      //     },
      //     err => {
      //       console.log(err);
      //     }
      //   );
      // } else {
      //   this.openSourceMark = "";
      //   this.getSame(id);
      // }
      this.openSourceMark = "";
      this.getSame(id);
    },
    // 封装获取摄像详细信息流量相关
    async getSame(id) {
      if (!(await this.getcams(id)) && id !== -1) {
        this.$emit("nowcameraurl", "downFlow");
        return;
      }
      this.cameraTimer = setInterval(async() => {
        if (!(await this.getcams(id)) && id !== -1) {
          this.$emit("nowcameraurl", "downFlow");
          clearInterval(this.cameraTimer)
        }
      }, 60 * 1000 * 10)
      nowpagereq.getcameramessage(id).then(res => {
        if (res.code === 1) {
          console.log(res.data);
          let data = res.data;
          this.cameraInfoList = data.camera;
          console.log("取得摄像头数据进行拼接");
          this.setcameraurl();
        }
      });
    },
    // 封装预热销毁函数
    // async closePreheat(id) {
    //   await Axios({
    //     method: "post",
    //     url: this.$cloudBaseurl + "solarEnergyCamera/closePower",
    //     data: { cameraId: id, openSourceMark: this.openSourceMark }
    //   }).then(
    //     res => {
    //       console.log(res);
    //       console.log("预热销毁成功");
    //     },
    //     err => {
    //       console.log(err);
    //     }
    //   );
    // },
    async getcams(id) {
      let zhurl =
        this.$cloudBaseurl +
        `cameraOperation/judgeFlow?cameraId=${id}`;
      const { data: res } = await Axios({
        method: "get",
        url: zhurl
      });
      // console.log(res)
      let data = res.data;
      if (res.code === 1) {
        // console.log(data.basis)
        if (data.flowUse) {
          this.$message.success(
            "总流量" + data.flowTotal + "已使用流量" + data.flowUse
          );
        }
        return data.basis;
      } else {
        this.$message.warning(res.msg || "未知错误！！！");
        return false;
      }
    },
    // 拼接摄像头地址
    setcameraurl() {
      // 判断有无摄像头
      if (this.cameraInfoList) {
        if (this.cameraInfoList.cameraInfo.status === 1) {
          // alert(this.cameraInfoList['deviceSerial'])
          this.cameraurl = "";
          // console.log(this.cameraInfoList)
          if (this.cameraInfoList) {
            this.cameraurl = `https://open.ys7.com/ezopen/h5/iframe?url=ezopen://open.ys7.com/${this.cameraInfoList["deviceSerial"]}/${this.cameraInfoList["channelNo"]}.hd.live&autoplay=1&accessToken=${this.cameraInfoList["accessToken"]}`;
          }
          // this.$emit("nowcameraurl", this.cameraurl);
          this.$emit("nowcameraurl", this.cameraurl, { id: 'video-container', accessToken: this.cameraInfoList["accessToken"], url: `ezopen://open.ys7.com/${this.cameraInfoList["deviceSerial"]}/${this.cameraInfoList["channelNo"]}.hd.live` });

          this.timer = setTimeout(() => {
            this.$emit("nowcameraurl", "timeout");
          }, 1000 * 60 * 20);
        } else {
          this.$message.warning("摄像头不在线！请稍后重试");
          this.$emit("nowcameraurl", "notCameraData");
          return;
        }
      } else {
        // 无 传空
        // this.cameraurl = null
        this.$emit("nowcameraurl", "notCameraList");
      }
    },
    // 开始控制
    async startcontrol(i) {
      this.timer = setTimeout(() => {
        this.$emit("nowcameraurl", "timeout");
      }, 1000 * 60 * 20);
      let params = {
        cameraId: this.cameraId,
        direction: i,
        speed: 1
      };
      await nowpagereq.controlcamera(params).then(res => {
        if (res.code != 200) this.$message.error(res.msg);
      });
      setTimeout(() => {
        this.stopcontrol();
      }, 1000);
    },
    // 镜像翻转
    async mirrorcontrol() {
      this.timer = setTimeout(() => {
        this.$emit("nowcameraurl", "timeout");
      }, 1000 * 60 * 20);
      let params = {
        cameraId: this.cameraId,
        direction: 2,
        speed: 1
      };
      await nowpagereq.mirrorcamera(params);
      setTimeout(() => {
        this.stopcontrol();
      }, 1000);
    },
    // 停止控制
    stopcontrol() {
      let params = {
        cameraId: this.cameraId
      };
      nowpagereq.stopcamera(params).then(res => {
        // console.log(res)
      });
    }
  }
};
</script>
<style lang="scss" scoped>
$height: 40px;
.uncontrolsty {
  width: 100%;
  height: calc(70% - 10px);
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  .uncontrolsty-line {
    color: #ccc;
    font-size: 30px;
    width: 30px;
    line-height: 50px;
  }
}
.allcameramain {
  height: 100%;
  width: 100%;
  .one {
    height: 30px;
    line-height: 28px;
    margin-left: 10%;
    width: 80%;
    text-align: center;
    margin-top: 20px;
    font-size: 18px;
    background-image: linear-gradient(to right, #46c357, #46c357);
    border-radius: 5px;
    border: 1px solid #fff;
  }
}
.cameralistmain {
  width: 100%;
  height: 35%;
  display: flex;
  flex-flow: nowrap column;
  align-items: center;
  justify-content: center;
}
.cameracontrol {
  width: 100%;
  height: calc(70% - 10px);
  margin-top: 10px;
  .controne {
    width: 100%;
    height: 65%;
    background-image: url("~@/assets/camera/ctrlBg.png");
    background-repeat: no-repeat;
    background-size: 90%;
    background-position: center;
    position: relative;
    .contro-m {
      // border: #46c357 1px solid;
      position: relative;
      width: 25%;
      height: 25%;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 40px;
      cursor: pointer;
    }
    .contro-m:hover {
      font-size: 48px;
    }
    .z-top {
      top: 10%;
      left: 37%;
    }
    .z-left {
      top: 12%;
      left: 11%;
    }
    .z-right {
      top: -13%;
      left: 63%;
    }
    .z-bottom {
      top: -10%;
      left: 37%;
    }
    .z-mirror {
      top: -63%;
      left: 37%;
    }
  }
  .controtwo {
    width: 100%;
    height: 15%;
    display: flex;
    justify-content: space-around;
    .contro-left {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformLeft.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-right: 11%;
      cursor: pointer;
      font-size: 18px;
      text-align: center;
    }
    .contro-left:hover {
      font-size: 20px;
    }
    .contro-right {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformRight.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-left: 11%;
      cursor: pointer;
      font-size: 18px;
    }
    .contro-right:hover {
      font-size: 20px;
    }
  }
  .controthree {
    width: 100%;
    height: 17%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .greenbut {
      width: 28%;
      height: 60%;
      background-color: rgb(132, 180, 111);
      border-radius: 5px;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 600;
      font-size: 22px;
      cursor: pointer;
    }
    .greenbut:hover {
      font-size: 24px;
    }
  }
}
.cameracontrol1 {
  width: 100%;
  height: calc(70% - 10px);
  margin-top: 10px;
  .controne {
    width: 100%;
    height: 65%;
    background-image: url("~@/assets/camera/ctrlBg.png");
    background-repeat: no-repeat;
    background-size: 90%;
    background-position: center;
    position: relative;
    mix-blend-mode: soft-light;
    .contro-m {
      // border: #46c357 1px solid;
      position: relative;
      width: 25%;
      height: 25%;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 40px;
      cursor: pointer;
    }
    .z-top {
      top: 10%;
      left: 37%;
    }
    .z-left {
      top: 12%;
      left: 11%;
    }
    .z-right {
      top: -13%;
      left: 63%;
    }
    .z-bottom {
      top: -10%;
      left: 37%;
    }
    .z-mirror {
      top: -63%;
      left: 37%;
    }
  }
  .controtwo {
    width: 100%;
    height: 15%;
    display: flex;
    justify-content: space-around;
    .contro-left {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformLeft.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-right: 11%;
      mix-blend-mode: soft-light;
      cursor: pointer;
      font-size: 18px;
    }
    .contro-right {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformRight.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-left: 11%;
      mix-blend-mode: soft-light;
      cursor: pointer;
      font-size: 18px;
    }
  }
  .controthree {
    width: 100%;
    height: 17%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    mix-blend-mode: soft-light;
    .greenbut {
      width: 28%;
      height: 60%;
      background-color: rgb(132, 180, 111);
      border-radius: 5px;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 600;
      font-size: 22px;
      cursor: pointer;
    }
  }
}
.zlistbtn {
  display: flex;
  justify-content: center;
  align-items: center;
  .btn {
    border: none !important;
    height: 100%;
  }
}
</style>
